/*
 * Doctor.java
 * 
 * @version 1.0
 * CS 2340 Team 10: Cyberdyne Systems
 */

package edu.gatech.oad.cyberdoc.domain;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import edu.gatech.oad.cyberdoc.domain.action.ActionHandleException;
import edu.gatech.oad.cyberdoc.domain.action.ActionHandler;

/**
 * This class represents a staff member who can add doctor's orders to treatment
 * records and view reports and statistics.
 * 
 * @author Jessie McGarry
 * @version 1.0
 * 
 */
public class Doctor extends Staff {

	/**
	 * The logger for this class.
	 */
	private static final Logger LOG;

	/**
	 * The handlers for the actions a Patient can perform.
	 */
	private static Map<String, ActionHandler> Handlers;

	static {
		LOG = Logger.getLogger("edu.gatech.oad.cyberdoc.domain.Patient");
		Handlers = new HashMap<String, ActionHandler>();
	}

	/**
	 * Creates a new doctor user in the system who has the specified username
	 * and password
	 * 
	 * @param username
	 *            - of the doctor to be created
	 * @param password
	 *            - of the doctor to be created
	 */
	public Doctor(String username, String password) {
		super(username, password);
	}

	/**
	 * Creates an instance of this class from a JSONObject, as required by
	 * DatabaseSerializable.
	 * 
	 * @param object
	 *            The information from the database to use to create this User
	 */
	public Doctor(JSONObject object) {
		super(object);
	}

	/**
	 * Handles the actions that a Patient can perform.
	 * 
	 * @param command
	 *            The command passed from the user
	 * @return The result of handling the command
	 * @throws ActionHandleException
	 *             If an ActionHandleException is thrown by the specific
	 *             ActionHandler
	 * @throws UnsupportedOperationException
	 *             If the action specified in the command is not recognized
	 */
	@Override
	public JSONObject handle(JSONObject command) throws ActionHandleException,
			UnsupportedOperationException {
		JSONObject response = null;

		try {
			final ActionHandler handler = Handlers.get(command
					.getString("action"));

			if (handler != null) {
				response = handler.handle(this, command);
			} else {
				throw new UnsupportedOperationException("Action "
						+ command.getString("action") + " not supported.");
			}
		} catch (JSONException e) {
			LOG.severe("Required field 'action' does not exist in command.");
			throw new ActionHandleException(e);
		}

		return response;
	}

	/**
	 * Registers an ActionHandler with this class.
	 * 
	 * @param action
	 *            The action codes to register to
	 * @param handler
	 *            The handler to respond to the action code
	 */
	public static void addActionHandler(String action, ActionHandler handler) {
		Handlers.put(action, handler);
	}
}
